<!DOCTYPE html>
<html  lang="zh-CN" >
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="theme-color" content="#fff" id="theme-color">
  <meta name="description" content="深山磨柴斧，一鸣震江湖">
  <link rel="icon" href="/img/ic_head.jpg">
  <title>一文搞定NGinx安装&使用</title>
  
  
  <meta property="og:title" content="一文搞定NGinx安装&amp;使用">
  
  
  <meta property="og:url" content="http://example.com/2018/08/19/%E4%B8%80%E6%96%87%E6%90%9E%E5%AE%9ANGinx%E5%AE%89%E8%A3%85&amp;%E4%BD%BF%E7%94%A8/index.html">
  
  
  <meta property="og:img" content="/img/ic_head.jpg">
  
  
  <meta property="og:img" content="深山磨柴斧，一鸣震江湖">
  
  
  <meta property="og:type" content="article">
  <meta property="og:article:published_time" content="2018-08-19">
  <meta property="og:article:modified_time" content="2021-04-14">
  <meta property="og:article:author" content="Cupster">
  
  
  <meta property="og:article:tag" content="Linux">
  
  <meta property="og:article:tag" content="CentOS">
  
  <meta property="og:article:tag" content="NGinx">
  
  
  
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
    var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
      }
    };
    setDarkmode();
  </script>
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
  </script>
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
  <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  
<link rel="stylesheet" href="/css/main.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">

  
  
<link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">

  
  
  
  
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <div class="wrapper">
    
    <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
      <img class="navbar-logo-img" src="/img/ic_head.jpg" alt="logo">
      
      <span class="navbar-logo-dsc">不负明天</span>
    </span>
  </div>
  <div class="navbar-menu">
    
    <a href="/" class="navbar-menu-item">
    
    首页
    
    </a>
    
    <a href="/archives" class="navbar-menu-item">
    
    归档
    
    </a>
    
    <a href="/tags" class="navbar-menu-item">
    
    标签
    
    </a>
    
    <a href="/categories" class="navbar-menu-item">
    
    分类
    
    </a>
    
    <a href="/about" class="navbar-menu-item">
    
    关于
    
    </a>
    
    <a href="/links" class="navbar-menu-item">
    
    友链
    
    </a>
    
    <a class="navbar-menu-item darknavbar" id="dark"><i class="iconfont icon-weather"></i></a>
    <a class="navbar-menu-item searchnavbar" id="search"><i class="iconfont icon-search" style="font-size: 1.2rem; font-weight: 400;"></i></a>
  </div>
</nav>
    
    <div id="local-search" style="display: none;">
      <input class="navbar-menu-item" id="search-input" placeholder="请输入搜索内容...">
      <div id="search-content"></div>
    </div>
    
    <div class="section-wrap">
      <div class="container">
        <div class="columns">
          <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      一文搞定NGinx安装&使用
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2018-08-19T04:15:25.000Z">
      <i class="iconfont icon-calendar" style="margin-right: 2px;"></i>
      <span>2018-08-19</span>
    </time>
    
    <span class="dot"></span>
    
    <a href="/categories/Linux/" class="post-meta-link">Linux</a>
    
    
    
    <span class="dot"></span>
    <span>1.1k 字</span>
    
  </div>
  
  <div class="post-meta post-show-meta" style="margin-top: -10px;">
    <div style="display: flex; align-items: center;">
      <i class="iconfont icon-biaoqian" style="margin-right: 2px; font-size: 1.15rem;"></i>
      
      
        <a href="/tags/Linux/" class="post-meta-link">Linux</a>
      
      
      <span class="dot"></span>
      
        <a href="/tags/CentOS/" class="post-meta-link">CentOS</a>
      
      
      <span class="dot"></span>
      
        <a href="/tags/NGinx/" class="post-meta-link">NGinx</a>
      
    </div>
  </div>
  
  </header>
  <div id="section" class="post-content">
    <p>学无止境，不负明天</p>
<hr/>

<h1 id="1-作用"><a href="#1-作用" class="headerlink" title="1.作用"></a>1.作用</h1><ul>
<li>反向代理服务器 &amp; 负载均衡</li>
<li>HTTP静态服务器</li>
<li>虚拟主机</li>
</ul>
<h1 id="2-安装nginx基础必备件"><a href="#2-安装nginx基础必备件" class="headerlink" title="2.安装nginx基础必备件"></a>2.安装nginx基础必备件</h1><p>本文基于CentOS7系统，讲解NGinx安装及使用。其他系统请自行<a href="www.baidu.com">百度</a></p>
<h2 id="2-1-c编译器：GCC"><a href="#2-1-c编译器：GCC" class="headerlink" title="2.1.c编译器：GCC"></a>2.1.c编译器：GCC</h2><p>nginx是C语言编写，安装nginx需要gcc编译nginx源码</p>
<pre><code>gcc -v
</code></pre>
<p>检查gcc版本，若无，则会提示找不到,需自行安装 gcc</p>
<pre><code>yum -y install gcc
</code></pre>
<p>若存在则会显示版本有关信息</p>
<img src="https:/ srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/20210408223329_OMiRrB.png" class="lozad post-image"/gitee.com/cupster/picbed/raw/master/20210408223329_OMiRrB.png"/>

<h2 id="2-2-安装解析库：pcre"><a href="#2-2-安装解析库：pcre" class="headerlink" title="2.2.安装解析库：pcre"></a>2.2.安装解析库：pcre</h2><p>nginx的http模块使用pcre来解析正则表达式，所以需要安装pcre库</p>
<pre><code>yum install -y pcre pcre-devel
</code></pre>
<img src="https:/ srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/20210408223938_fOfQqL.png" class="lozad post-image"/gitee.com/cupster/picbed/raw/master/20210408223938_fOfQqL.png"/>

<h2 id="2-3-安装压缩工具：zlib"><a href="#2-3-安装压缩工具：zlib" class="headerlink" title="2.3.安装压缩工具：zlib"></a>2.3.安装压缩工具：zlib</h2><p>nginx使用zlib对http包的内容进行gzip</p>
<pre><code>yum install -y zlib zlib-devel
</code></pre>
<img src="https:/ srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/20210408224006_8STez6.png" class="lozad post-image"/gitee.com/cupster/picbed/raw/master/20210408224006_8STez6.png"/>

<h2 id="2-4-安装openssl"><a href="#2-4-安装openssl" class="headerlink" title="2.4.安装openssl"></a>2.4.安装openssl</h2><p>网络通信加密</p>
<pre><code>yum install -y openssl openssl-devel
</code></pre>
<img src="https:/ srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/20210408224243_VSlsXQ.png" class="lozad post-image"/gitee.com/cupster/picbed/raw/master/20210408224243_VSlsXQ.png"/>


<h1 id="3-安装NGinx"><a href="#3-安装NGinx" class="headerlink" title="3.安装NGinx"></a>3.安装NGinx</h1><h2 id="3-1-下载"><a href="#3-1-下载" class="headerlink" title="3.1.下载"></a>3.1.下载</h2><p>cd进入指定存放路径</p>
<pre><code>cd /tmp
</code></pre>
<p>下载至当前目录 tmp</p>
<pre><code>wget http://nginx.org/download/nginx-1.9.9.tar.gz  
</code></pre>
<p>解压到 /usr/local/java</p>
<pre><code>tar -zxvf nginx-1.9.9.tar.gz -C /usr/local/java
</code></pre>
<p>cd进入 /usr/local/java</p>
<pre><code>cd /usr/local/java
</code></pre>
<img src="https:/ srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/20210408225135_dTBWks.png" class="lozad post-image"/gitee.com/cupster/picbed/raw/master/20210408225135_dTBWks.png"/>

<h2 id="3-2-编译-amp-安装"><a href="#3-2-编译-amp-安装" class="headerlink" title="3.2.编译&amp;安装"></a>3.2.编译&amp;安装</h2><p>先进入nginx源码文件夹</p>
<pre><code>cd nginx-1.9.9/
</code></pre>
<p>执行</p>
<pre><code>./configure
</code></pre>
<img src="https:/ srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/20210408225325_0B7AUK.png" class="lozad post-image"/gitee.com/cupster/picbed/raw/master/20210408225325_0B7AUK.png"/>    

<p>再执行</p>
<pre><code>make
</code></pre>
<p>最后执行</p>
<pre><code>make install
</code></pre>
<h2 id="3-3-配置修改"><a href="#3-3-配置修改" class="headerlink" title="3.3.配置修改"></a>3.3.配置修改</h2><p>切换到nginx安装目录,修改所需监听的端口 或 配置域名等。</p>
<p>如无需要，可略过该步，使用默认配置</p>
<pre><code>cd /usr/local/nginx
</code></pre>
<img src="https:/ srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/20210408225859_xW4LaQ.png" class="lozad post-image"/gitee.com/cupster/picbed/raw/master/20210408225859_xW4LaQ.png"/>

<p>编辑配置文件：</p>
<pre><code>cd conf
vim nginx.conf
</code></pre>
<pre class="highlight"><span class="line"><span class="comment">#...</span></span><br><span class="line"><span class="comment">#虚拟主机</span></span><br><span class="line"><span class="string">server</span> &#123;</span><br><span class="line">    <span class="string">listen</span>       <span class="number">80</span><span class="string">;</span>  <span class="comment">#配置监听端口号</span></span><br><span class="line">    <span class="string">server_name</span>  <span class="string">localhost;</span> <span class="comment">#配置访问域名，域名可以有多个，用空格隔开</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">#charset koi8-r; #字符集设置</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">#access_log  logs/host.access.log  main;</span></span><br><span class="line"> </span><br><span class="line">    <span class="string">location</span> <span class="string">/</span> &#123;</span><br><span class="line">        <span class="string">root</span>   <span class="string">html;</span></span><br><span class="line">        <span class="string">index</span>  <span class="string">index.html</span> <span class="string">index.htm;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">#...</span></span><br><span class="line">    <span class="comment">#...</span></span><br><span class="line">    <span class="comment">#...</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">#...</span></span><br></pre>

<h1 id="4-使用Nginx"><a href="#4-使用Nginx" class="headerlink" title="4.使用Nginx"></a>4.使用Nginx</h1><h2 id="4-1启动Nginx服务"><a href="#4-1启动Nginx服务" class="headerlink" title="4.1启动Nginx服务"></a>4.1启动Nginx服务</h2><pre><code>cd /usr/local/nginx/sbin

./nginx
</code></pre>
<img src="https:/ srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/20210408231640_i6iUUX.png" class="lozad post-image"/gitee.com/cupster/picbed/raw/master/20210408231640_i6iUUX.png"/>


<h2 id="4-2查看Nginx运行状态"><a href="#4-2查看Nginx运行状态" class="headerlink" title="4.2查看Nginx运行状态"></a>4.2查看Nginx运行状态</h2><pre><code>ps -ef | grep nginx
</code></pre>
<img src="https:/ srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/20210408231916_YR8jHA.png" class="lozad post-image"/gitee.com/cupster/picbed/raw/master/20210408231916_YR8jHA.png"/>


<h2 id="4-3关闭"><a href="#4-3关闭" class="headerlink" title="4.3关闭"></a>4.3关闭</h2><p>强制停止：不管连接是否结束，都强制关闭Nginx</p>
<pre><code>./nginx -s stop
</code></pre>
<p>安全停止：等连接结束之后在关闭Nginx</p>
<pre><code>./nginx -s quit
</code></pre>
<h2 id="4-4重启"><a href="#4-4重启" class="headerlink" title="4.4重启"></a>4.4重启</h2><pre><code>./nginx -s reload
</code></pre>
<h2 id="5-NGinx配置详解"><a href="#5-NGinx配置详解" class="headerlink" title="5.NGinx配置详解"></a>5.NGinx配置详解</h2><pre class="highlight"><span class="line"><span class="comment">#user  nobody;</span></span><br><span class="line"><span class="string">worker_processes</span>  <span class="number">1</span><span class="string">;</span> <span class="comment">#工作进程：数目。根据硬件调整，通常等于cpu数量或者2倍cpu数量。</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">#错误日志存放路径</span></span><br><span class="line"><span class="comment">#error_log  logs/error.log;</span></span><br><span class="line"><span class="comment">#error_log  logs/error.log  notice;</span></span><br><span class="line"><span class="comment">#error_log  logs/error.log  info;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">#pid        logs/nginx.pid; # nginx进程pid存放路径</span></span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="string">events</span> &#123;</span><br><span class="line">    <span class="string">worker_connections</span>  <span class="number">1024</span><span class="string">;</span> <span class="comment"># 工作进程的最大连接数量</span></span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="string">http</span> &#123;</span><br><span class="line">    <span class="string">include</span>       <span class="string">mime.types;</span> <span class="comment">#指定mime类型，由mime.type来定义</span></span><br><span class="line">    <span class="string">default_type</span>  <span class="string">application/octet-stream;</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment"># 日志格式设置</span></span><br><span class="line">    <span class="comment">#log_format  main  &#x27;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &#x27;</span></span><br><span class="line">    <span class="comment">#                  &#x27;$status $body_bytes_sent &quot;$http_referer&quot; &#x27;</span></span><br><span class="line">    <span class="comment">#                  &#x27;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;&#x27;;</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">#access_log  logs/access.log  main; #用log_format指令设置日志格式后，需要用access_log来指定日志文件存放路径</span></span><br><span class="line">					</span><br><span class="line">    <span class="string">sendfile</span>        <span class="string">on;</span> <span class="comment">#指定nginx是否调用sendfile函数来输出文件，对于普通应用，必须设置on。</span></span><br><span class="line">			<span class="string">如果用来进行下载等应用磁盘io重负载应用，可设着off，以平衡磁盘与网络io处理速度，降低系统uptime。</span></span><br><span class="line">    <span class="comment">#tcp_nopush     on; #此选项允许或禁止使用socket的TCP_CORK的选项，此选项仅在sendfile的时候使用</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">#keepalive_timeout  0;  #keepalive超时时间</span></span><br><span class="line">    <span class="string">keepalive_timeout</span>  <span class="number">65</span><span class="string">;</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">#gzip  on; #开启gzip压缩服务</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">#虚拟主机</span></span><br><span class="line">    <span class="string">server</span> &#123;</span><br><span class="line">        <span class="string">listen</span>       <span class="number">80</span><span class="string">;</span>  <span class="comment">#配置监听端口号</span></span><br><span class="line">        <span class="string">server_name</span>  <span class="string">localhost;</span> <span class="comment">#配置访问域名，域名可以有多个，用空格隔开</span></span><br><span class="line"> </span><br><span class="line">        <span class="comment">#charset koi8-r; #字符集设置</span></span><br><span class="line"> </span><br><span class="line">        <span class="comment">#access_log  logs/host.access.log  main;</span></span><br><span class="line"> </span><br><span class="line">        <span class="string">location</span> <span class="string">/</span> &#123;</span><br><span class="line">            <span class="string">root</span>   <span class="string">html;</span></span><br><span class="line">            <span class="string">index</span>  <span class="string">index.html</span> <span class="string">index.htm;</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">#错误跳转页</span></span><br><span class="line">        <span class="comment">#error_page  404              /404.html; </span></span><br><span class="line"> </span><br><span class="line">        <span class="comment"># redirect server error pages to the static page /50x.html</span></span><br><span class="line">        <span class="comment">#</span></span><br><span class="line">        <span class="string">error_page</span>   <span class="number">500</span> <span class="number">502</span> <span class="number">503</span> <span class="number">504</span>  <span class="string">/50x.html;</span></span><br><span class="line">        <span class="string">location</span> <span class="string">=</span> <span class="string">/50x.html</span> &#123;</span><br><span class="line">            <span class="string">root</span>   <span class="string">html;</span></span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        <span class="comment"># proxy the PHP scripts to Apache listening on 127.0.0.1:80</span></span><br><span class="line">        <span class="comment">#</span></span><br><span class="line">        <span class="comment">#location ~ \.php$ &#123;</span></span><br><span class="line">        <span class="comment">#    proxy_pass   http://127.0.0.1;</span></span><br><span class="line">        <span class="comment">#&#125;</span></span><br><span class="line"> </span><br><span class="line">        <span class="comment"># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span></span><br><span class="line">        <span class="comment">#</span></span><br><span class="line">        <span class="comment">#location ~ \.php$ &#123; #请求的url过滤，正则匹配，~为区分大小写，~*为不区分大小写。</span></span><br><span class="line">        <span class="comment">#    root           html; #根目录</span></span><br><span class="line">        <span class="comment">#    fastcgi_pass   127.0.0.1:9000; #请求转向定义的服务器列表</span></span><br><span class="line">        <span class="comment">#    fastcgi_index  index.php; # 如果请求的Fastcgi_index URI是以 / 结束的, 该指令设置的文件会被附加到URI的后面并保存在变量$fastcig_script_name中</span></span><br><span class="line">        <span class="comment">#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;</span></span><br><span class="line">        <span class="comment">#    include        fastcgi_params;</span></span><br><span class="line">        <span class="comment">#&#125;</span></span><br><span class="line"> </span><br><span class="line">        <span class="comment"># deny access to .htaccess files, if Apache&#x27;s document root</span></span><br><span class="line">        <span class="comment"># concurs with nginx&#x27;s one</span></span><br><span class="line">        <span class="comment">#</span></span><br><span class="line">        <span class="comment">#location ~ /\.ht &#123;</span></span><br><span class="line">        <span class="comment">#    deny  all;</span></span><br><span class="line">        <span class="comment">#&#125;</span></span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">    <span class="comment"># another virtual host using mix of IP-, name-, and port-based configuration</span></span><br><span class="line">    <span class="comment">#</span></span><br><span class="line">    <span class="comment">#server &#123;</span></span><br><span class="line">    <span class="comment">#    listen       8000;</span></span><br><span class="line">    <span class="comment">#    listen       somename:8080;</span></span><br><span class="line">    <span class="comment">#    server_name  somename  alias  another.alias;</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">#    location / &#123;</span></span><br><span class="line">    <span class="comment">#        root   html;</span></span><br><span class="line">    <span class="comment">#        index  index.html index.htm;</span></span><br><span class="line">    <span class="comment">#    &#125;</span></span><br><span class="line">    <span class="comment">#&#125;</span></span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">    <span class="comment"># HTTPS server</span></span><br><span class="line">    <span class="comment">#</span></span><br><span class="line">    <span class="comment">#server &#123;</span></span><br><span class="line">    <span class="comment">#    listen       443 ssl;  #监听端口</span></span><br><span class="line">    <span class="comment">#    server_name  localhost; #域名</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">#    ssl_certificate      cert.pem; #证书位置</span></span><br><span class="line">    <span class="comment">#    ssl_certificate_key  cert.key; #私钥位置</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">#    ssl_session_cache    shared:SSL:1m;</span></span><br><span class="line">    <span class="comment">#    ssl_session_timeout  5m; </span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">#    ssl_ciphers  HIGH:!aNULL:!MD5; #密码加密方式</span></span><br><span class="line">    <span class="comment">#    ssl_prefer_server_ciphers  on; # ssl_prefer_server_ciphers  on; #</span></span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">    <span class="comment">#    location / &#123;</span></span><br><span class="line">    <span class="comment">#        root   html;</span></span><br><span class="line">    <span class="comment">#        index  index.html index.htm;</span></span><br><span class="line">    <span class="comment">#    &#125;</span></span><br><span class="line">    <span class="comment">#&#125;</span></span><br><span class="line"> </span><br><span class="line">&#125;</span><br></pre>










  </div>
  <div>
  
  <div class="post-note note-warning copyright" style="margin-top: 42px">
    <p><span style="font-weight: bold;">作者：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/about">Cupster</a></p>
    <p><span style="font-weight: bold;">文章链接：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/2018/08/19/%E4%B8%80%E6%96%87%E6%90%9E%E5%AE%9ANGinx%E5%AE%89%E8%A3%85&amp;%E4%BD%BF%E7%94%A8/">http://example.com/2018/08/19/%E4%B8%80%E6%96%87%E6%90%9E%E5%AE%9ANGinx%E5%AE%89%E8%A3%85&amp;%E4%BD%BF%E7%94%A8/</a></p>
    <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
  </div>
  
  </div>
</article>
<div class="nav">
  
  <div class="nav-item-prev">
    <a href="/2019/05/26/001-关于Android的一些基础知识详解/" class="nav-link">
      <i class="iconfont icon-left nav-prev-icon"></i>
      <div>
        <div class="nav-label">上一篇</div>
        
        <div class="nav-title">001-关于Android的一些基础知识详解 </div>
        
      </div>
    </a>
  </div>
  
  
  <div class="nav-item-next">
    <a href="/2018/08/09/服务器数据迁移：为知笔记私服数据迁移/" class="nav-link">
      <div>
        <div class="nav-label">下一篇</div>
        
        <div class="nav-title">服务器数据迁移：为知笔记私服数据迁移 </div>
        
      </div>
      <i class="iconfont icon-right nav-next-icon"></i>
    </a>
  </div>
  
</div>

<div class="card card-content toc-card" id="mobiletoc">
  <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E4%BD%9C%E7%94%A8"><span class="toc-text">1.作用</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E5%AE%89%E8%A3%85nginx%E5%9F%BA%E7%A1%80%E5%BF%85%E5%A4%87%E4%BB%B6"><span class="toc-text">2.安装nginx基础必备件</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-c%E7%BC%96%E8%AF%91%E5%99%A8%EF%BC%9AGCC"><span class="toc-text">2.1.c编译器：GCC</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E5%AE%89%E8%A3%85%E8%A7%A3%E6%9E%90%E5%BA%93%EF%BC%9Apcre"><span class="toc-text">2.2.安装解析库：pcre</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E5%AE%89%E8%A3%85%E5%8E%8B%E7%BC%A9%E5%B7%A5%E5%85%B7%EF%BC%9Azlib"><span class="toc-text">2.3.安装压缩工具：zlib</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E5%AE%89%E8%A3%85openssl"><span class="toc-text">2.4.安装openssl</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%AE%89%E8%A3%85NGinx"><span class="toc-text">3.安装NGinx</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-%E4%B8%8B%E8%BD%BD"><span class="toc-text">3.1.下载</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-%E7%BC%96%E8%AF%91-amp-%E5%AE%89%E8%A3%85"><span class="toc-text">3.2.编译&amp;安装</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-%E9%85%8D%E7%BD%AE%E4%BF%AE%E6%94%B9"><span class="toc-text">3.3.配置修改</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E4%BD%BF%E7%94%A8Nginx"><span class="toc-text">4.使用Nginx</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1%E5%90%AF%E5%8A%A8Nginx%E6%9C%8D%E5%8A%A1"><span class="toc-text">4.1启动Nginx服务</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2%E6%9F%A5%E7%9C%8BNginx%E8%BF%90%E8%A1%8C%E7%8A%B6%E6%80%81"><span class="toc-text">4.2查看Nginx运行状态</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3%E5%85%B3%E9%97%AD"><span class="toc-text">4.3关闭</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-4%E9%87%8D%E5%90%AF"><span class="toc-text">4.4重启</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-NGinx%E9%85%8D%E7%BD%AE%E8%AF%A6%E8%A7%A3"><span class="toc-text">5.NGinx配置详解</span></a></li></ol></li></ol>
</div></main>
          <aside class="left-column">
            
            <div class="card card-author">
              
<img src="/img/ic_head.jpg" class="author-img">

<p class="author-name">Cupster</p>
<p class="author-description">深山磨柴斧，一鸣震江湖</p>
<div class="author-message">
  <a class="author-posts-count" href="/archives">
    <span>66</span>
    <span>文章</span>
  </a>
  <a class="author-categories-count" href="/categories">
    <span>5</span>
    <span>分类</span>
  </a>
  <a class="author-tags-count" href="/tags">
    <span>31</span>
    <span>标签</span>
  </a>
</div>

<div class="author-card-society">
  
    <div class="author-card-society-icon">
      <a target="_blank" rel="noopener" href="https://cupster.blog.csdn.net/">
        <i class="iconfont /img/ic_csdn.png society-icon"></i>
      </a>
    </div>
  
</div>

            </div>
            
            <div class="sticky-tablet">
  
  
  <article class="display-when-two-columns spacer">
    <div class="card card-content toc-card">
      <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E4%BD%9C%E7%94%A8"><span class="toc-text">1.作用</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E5%AE%89%E8%A3%85nginx%E5%9F%BA%E7%A1%80%E5%BF%85%E5%A4%87%E4%BB%B6"><span class="toc-text">2.安装nginx基础必备件</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-c%E7%BC%96%E8%AF%91%E5%99%A8%EF%BC%9AGCC"><span class="toc-text">2.1.c编译器：GCC</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E5%AE%89%E8%A3%85%E8%A7%A3%E6%9E%90%E5%BA%93%EF%BC%9Apcre"><span class="toc-text">2.2.安装解析库：pcre</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E5%AE%89%E8%A3%85%E5%8E%8B%E7%BC%A9%E5%B7%A5%E5%85%B7%EF%BC%9Azlib"><span class="toc-text">2.3.安装压缩工具：zlib</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E5%AE%89%E8%A3%85openssl"><span class="toc-text">2.4.安装openssl</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%AE%89%E8%A3%85NGinx"><span class="toc-text">3.安装NGinx</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-%E4%B8%8B%E8%BD%BD"><span class="toc-text">3.1.下载</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-%E7%BC%96%E8%AF%91-amp-%E5%AE%89%E8%A3%85"><span class="toc-text">3.2.编译&amp;安装</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-%E9%85%8D%E7%BD%AE%E4%BF%AE%E6%94%B9"><span class="toc-text">3.3.配置修改</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E4%BD%BF%E7%94%A8Nginx"><span class="toc-text">4.使用Nginx</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1%E5%90%AF%E5%8A%A8Nginx%E6%9C%8D%E5%8A%A1"><span class="toc-text">4.1启动Nginx服务</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2%E6%9F%A5%E7%9C%8BNginx%E8%BF%90%E8%A1%8C%E7%8A%B6%E6%80%81"><span class="toc-text">4.2查看Nginx运行状态</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3%E5%85%B3%E9%97%AD"><span class="toc-text">4.3关闭</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-4%E9%87%8D%E5%90%AF"><span class="toc-text">4.4重启</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-NGinx%E9%85%8D%E7%BD%AE%E8%AF%A6%E8%A7%A3"><span class="toc-text">5.NGinx配置详解</span></a></li></ol></li></ol>
    </div>
  </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header"><i class="iconfont icon-fenlei" style="padding-right: 2px;"></i>分类</div>
  <div class="categories-list">
    
      <a href="/categories/Android/">
        <div class="categories-list-item">
          Android
          <span class="categories-list-item-badge">38</span>
        </div>
      </a>
    
      <a href="/categories/Linux/">
        <div class="categories-list-item">
          Linux
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Dev-Base/">
        <div class="categories-list-item">
          Dev-Base
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/Kotlin/">
        <div class="categories-list-item">
          Kotlin
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/自动化脚本/">
        <div class="categories-list-item">
          自动化脚本
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header"><i class="iconfont icon-biaoqian" style="padding-right: 2px;"></i>热门标签</div>
  <div class="tags-list">
    
    <a href="/tags/Android/" title="Android"><div class="tags-list-item">Android</div></a>
    
    <a href="/tags/Android自定义View/" title="Android自定义View"><div class="tags-list-item">Android自定义View</div></a>
    
    <a href="/tags/MacOS/" title="MacOS"><div class="tags-list-item">MacOS</div></a>
    
    <a href="/tags/Kotlin/" title="Kotlin"><div class="tags-list-item">Kotlin</div></a>
    
    <a href="/tags/Android持久化/" title="Android持久化"><div class="tags-list-item">Android持久化</div></a>
    
    <a href="/tags/CentOS/" title="CentOS"><div class="tags-list-item">CentOS</div></a>
    
    <a href="/tags/Linux/" title="Linux"><div class="tags-list-item">Linux</div></a>
    
    <a href="/tags/Android事件/" title="Android事件"><div class="tags-list-item">Android事件</div></a>
    
    <a href="/tags/Android基础/" title="Android基础"><div class="tags-list-item">Android基础</div></a>
    
    <a href="/tags/Android手势/" title="Android手势"><div class="tags-list-item">Android手势</div></a>
    
    <a href="/tags/Scrcpy/" title="Scrcpy"><div class="tags-list-item">Scrcpy</div></a>
    
    <a href="/tags/Android动画/" title="Android动画"><div class="tags-list-item">Android动画</div></a>
    
    <a href="/tags/Git/" title="Git"><div class="tags-list-item">Git</div></a>
    
    <a href="/tags/Java/" title="Java"><div class="tags-list-item">Java</div></a>
    
    <a href="/tags/Docker/" title="Docker"><div class="tags-list-item">Docker</div></a>
    
    <a href="/tags/Android-Dialog/" title="Android Dialog"><div class="tags-list-item">Android Dialog</div></a>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
          <aside class="right-column">
            <div class="sticky-widescreen">
  
  
  <article class="card card-content toc-card">
    <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E4%BD%9C%E7%94%A8"><span class="toc-text">1.作用</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E5%AE%89%E8%A3%85nginx%E5%9F%BA%E7%A1%80%E5%BF%85%E5%A4%87%E4%BB%B6"><span class="toc-text">2.安装nginx基础必备件</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-c%E7%BC%96%E8%AF%91%E5%99%A8%EF%BC%9AGCC"><span class="toc-text">2.1.c编译器：GCC</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E5%AE%89%E8%A3%85%E8%A7%A3%E6%9E%90%E5%BA%93%EF%BC%9Apcre"><span class="toc-text">2.2.安装解析库：pcre</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E5%AE%89%E8%A3%85%E5%8E%8B%E7%BC%A9%E5%B7%A5%E5%85%B7%EF%BC%9Azlib"><span class="toc-text">2.3.安装压缩工具：zlib</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E5%AE%89%E8%A3%85openssl"><span class="toc-text">2.4.安装openssl</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%AE%89%E8%A3%85NGinx"><span class="toc-text">3.安装NGinx</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-%E4%B8%8B%E8%BD%BD"><span class="toc-text">3.1.下载</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-%E7%BC%96%E8%AF%91-amp-%E5%AE%89%E8%A3%85"><span class="toc-text">3.2.编译&amp;安装</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-%E9%85%8D%E7%BD%AE%E4%BF%AE%E6%94%B9"><span class="toc-text">3.3.配置修改</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E4%BD%BF%E7%94%A8Nginx"><span class="toc-text">4.使用Nginx</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1%E5%90%AF%E5%8A%A8Nginx%E6%9C%8D%E5%8A%A1"><span class="toc-text">4.1启动Nginx服务</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2%E6%9F%A5%E7%9C%8BNginx%E8%BF%90%E8%A1%8C%E7%8A%B6%E6%80%81"><span class="toc-text">4.2查看Nginx运行状态</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3%E5%85%B3%E9%97%AD"><span class="toc-text">4.3关闭</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-4%E9%87%8D%E5%90%AF"><span class="toc-text">4.4重启</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-NGinx%E9%85%8D%E7%BD%AE%E8%AF%A6%E8%A7%A3"><span class="toc-text">5.NGinx配置详解</span></a></li></ol></li></ol>
  </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header"><i class="iconfont icon-wenzhang_huaban" style="padding-right: 2px;"></i>最近文章</div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-03-02</div>
        <a href="/2021/03/02/Git commit规范/"><div class="recent-posts-item-content">Git commit规范</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-02-16</div>
        <a href="/2021/02/16/AutoJS一文精通AutoJS脚本教程详解/"><div class="recent-posts-item-content">AutoJS一文精通AutoJS脚本教程详解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-21</div>
        <a href="/2021/01/21/架构师进阶：注解Annotation全解/"><div class="recent-posts-item-content">架构师进阶：注解Annotation全解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-06</div>
        <a href="/2021/01/06/ConstraintLayout详解/"><div class="recent-posts-item-content">ConstraintLayout详解</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
        </div>
      </div>
    </div>
  </div>
  
  <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>Copyright ©
          
          2020 -
          
          2021
        </span>
        &nbsp;
        <a href="/" class="footer-link">不负明天 </a>
      </div>
    </div>

    
    <div class="footer-dsc">
      
      Powered by
      <a href="https://hexo.io/" class="footer-link" target="_blank" rel="nofollow noopener noreferrer">&nbsp;Hexo </a>
      
      
      <span>&nbsp;|&nbsp;</span>
      
      
      Theme -
      <a href="https://github.com/theme-kaze" class="footer-link" target="_blank"
        rel="nofollow noopener noreferrer">&nbsp;Kaze</a>
      
    </div>
    
    
    
    
      <div class="footer-dsc">
        
        本站总访问量686<span id="busuanzi_value_site_pv"></span>次
        
        
        <span>&nbsp;|&nbsp;</span>
        
        
        本站总访客数219<span id="busuanzi_value_site_uv"></span>次
        
      </div>
      
    
</footer>
  <a role="button" id="scrollbutton" class="basebutton"  aria-label="回到顶部">
  <i class="iconfont icon-arrowleft button-icon"></i>
</a>
<a role="button" id="menubutton" class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a role="button" id="popbutton" class="basebutton" aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a role="button" id="darkbutton" class="basebutton darkwidget" aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a role="button" id="searchbutton" class="basebutton searchwidget" aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a>

  
  
  
  <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img');
    var i;
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a');
      wrapper.setAttribute('href', img[i].getAttribute('data-src'));
      wrapper.setAttribute('aria-label', 'illustration');
      wrapper.style.cssText = 'width: 100%; display: flex; justify-content: center;';
      if (img[i].alt) wrapper.dataset.caption = img[i].alt;
      wrapper.dataset.nolink = true;
      img[i].before(wrapper);
      wrapper.append(img[i]);
      var divWrap = document.createElement('div');
      divWrap.classList.add('gallery');
      wrapper.before(divWrap);
      divWrap.append(wrapper);
    }
    baguetteBox.run('.gallery');
  }
</script>
<script>loadScript("/js/lib/lightbox/baguetteBox.min.js", addImgLayout)</script>
  
  
  
<script src="/js/main.js"></script>

  
  <script>loadScript("/js/lib/busuanzi.min.js")</script>
  
  
  <script>
    var addLazyload = function () {
      var observer = lozad('.lozad', {
        load: function (el) {
          el.srcset = el.getAttribute('data-src');
        },
        loaded: function (el) {
          el.classList.add('loaded');
        }
      });
      observer.observe();
    }
  </script>
  <script>loadScript("/js/lib/lozad.min.js", addLazyload)</script>
  
  
</body>

</html>